package com.wanlian.dao.impl;

import com.wanlian.common.PageCondition;
import com.wanlian.dao.QuestionDao;
import com.wanlian.entity.Question;
import com.wanlian.entity.QuestionType;
import com.wanlian.model.ResultModel;
import com.wanlian.util.StringUtil;
import com.wanlian.vo.Page;
import com.wanlian.vo.QuestionVo;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Component
public class QuestionDaoImpl extends GenericDaoImpl<Question> implements QuestionDao {
    public QuestionDaoImpl() {
        super(Question.class);
    }

    @Override
    public ResultModel queryMessage(Page page, Map<String, Object> conditions) {
        StringBuilder sqlWhere = new StringBuilder(" where 1=1 ");
        ArrayList<Object> param = new ArrayList<Object>();
        if (conditions != null) {
            if (conditions.containsKey("title")) {
                String title = (String) conditions.get("title");
                if (StringUtil.hasValue(title)) {
                    sqlWhere.append(" and title like ?");
                    param.add("%" + title + "%");
                }
            }
            if (conditions.containsKey("typeId")) {
                try {
                    int typeId = (int) conditions.get("typeId");
                    if (StringUtil.hasValue(typeId + "")) {
                        sqlWhere.append(" and typeId=?");
                        param.add(typeId);
                    }
                } catch (Exception e) {
                    System.out.println("为非数字" + e.toString());
                }

            }
            if (conditions.containsKey("lastUpdateTime")) {
                String lastUpdateTime = (String) conditions.get("lastUpdateTime");
                if (StringUtil.hasValue(lastUpdateTime)) {
                    sqlWhere.append(" and lastUpdateTime =date(?)");
                    param.add(lastUpdateTime);
                }
            }
        }
        List<Question> lists = new ArrayList<Question>();
        String hql = String.format("from %s " + sqlWhere, entityClassName());
        lists = this.queryForListByPage(hql, param.toArray(), page);
        String countSql = String.format("select count(*) from %s" + sqlWhere, entityClassName());
        long count = this.queryCount(countSql, param.toArray());
        ResultModel resultModel = new ResultModel(true, lists, count);
        return resultModel;
    }


    @Override
    public List<Question> queryQuestionsByParams(Map<String, Object> conditions) {
        StringBuilder sqlWhere = new StringBuilder(" where 1=1 ");
        ArrayList<Object> param = new ArrayList<Object>();

        int typeid=-1;
        try {
            typeid = (int)conditions.get("questionTypeId");
        } catch (Exception e) {
           typeid=0;
        }
        if(typeid!=0){
            sqlWhere.append(" and typeId=?");
            param.add(typeid);
        }

        int userId=-1;
        try {
            userId = (int)conditions.get("userId");
        } catch (Exception e) {
            userId=0;
        }
        if(userId!=0){
            sqlWhere.append(" and userid=?");
            param.add(userId);
        }



        List<Question> lists = new ArrayList<Question>();
        String hql = String.format("from %s " + sqlWhere, entityClassName());
        lists = this.queryForList(hql, param.toArray());
        return lists;
    }

    @Override
    public long queryQuestionCountByTypeId(int id) {
        StringBuilder sqlWhere = new StringBuilder(" where 1=1 ");
        ArrayList<Object> param = new ArrayList<Object>();
        sqlWhere.append(" and typeid=?");
        param.add(id);
        String countSql = String.format("select count(*) from %s" + sqlWhere, entityClassName());
        long count = this.queryCount(countSql, param.toArray());
        return count;
    }


    @Override
    public ResultModel queryQueAnsByParams(PageCondition pageCondition) {
        String sql="select * from( select q.remark,q.imgpath,qt.mname,q.id as qid,q.content,q.title,q.lastupdatetime quetime,u.username ,qa.content as anscontent," +
                " (case ifnull(qa.content,1) when 1 then 0 else  1 end )as  state,qa.lastupdatetime as ahstime" +
                " from tb_question q left join tb_questiontype qt on q.typeid=qt.id" +
                " left join tb_user u on q.userid=u.id" +
                " left join tb_questionanswer  qa on qa.qid=q.id) a where 1=1 ";
        StringBuilder sqlWhere=new StringBuilder();
        String[] column = {"remark","imgpath","mname","qid","content", "title", "quetime", "username", "anscontent","state", "anstime"};
        ArrayList<Object> param = new ArrayList<Object>();
        if (pageCondition.isEmpty("username")) {
            String username = (String) pageCondition.getByKey("username");
            sqlWhere.append(" and a.username like ? ");
            param.add("%" + username + "%");
        }
        if (pageCondition.isEmpty("mname")) {
            String mname = (String) pageCondition.getByKey("mname");
            sqlWhere.append(" and a.mname=? ");
            param.add(mname);
        }

        if (pageCondition.isEmpty("state")) {
            int state = Integer.parseInt((String) pageCondition.getByKey("state"));
            sqlWhere.append(" and a.state=? ");
            param.add(state);
        }

        if (pageCondition.isEmpty("startTime")&&pageCondition.isEmpty("endTime")) {
            String startTime = (String) pageCondition.getByKey("startTime");
            String endTime = (String) pageCondition.getByKey("endTime");
            sqlWhere.append(" and a.quetime between date(?) and date(?) ");
            param.add(startTime);
            param.add(endTime);
        }
        sqlWhere.append(" order by a.quetime desc  ");
        List<Map<String, Object>> lists = this.queryForListObjectByPage(sql+sqlWhere + "", param.toArray(), pageCondition.getPage(), column);
        int count =this.queryCountByParam("select count(1) from ( select * from( select qt.mname,q.id as qid,q.content,q.title,q.lastupdatetime quetime,u.username ,qa.content as anscontent,(case ifnull(qa.content,1) when 1 then 0 else  1 end )as  state,qa.lastupdatetime as ahstime " +
                "                 from tb_question q left join tb_questiontype qt on q.typeid=qt.id " +
                "                 left join tb_user u on q.userid=u.id " +
                "                 left join tb_questionanswer  qa on qa.qid=q.id) a where 1=1 "+sqlWhere+")b  ",param.toArray());
        ResultModel resultModel = new ResultModel(true, lists, count);
        return resultModel;
    }
}
